﻿using LD.Host;
using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace LD
{
    public class UserHelper
    {
        public delegate void LoginHandler(UserModel user);
        public event LoginHandler UserChangeEvent;
        private string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config\\User.config");
        private static UserHelper instance;
        private static object locker = new object();
        public static UserHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new UserHelper();
                        }
                    }
                }

                return instance;
            }
        }
        private UserHelper()
        {
            //Init();
            Load();
        }
        public void Load()
        {
            if (File.Exists(filePath))
            {
                users = (List<UserModel>)SerializableHelper.DeSerialize(filePath);
            }

            Current = new UserModel() { Name = "操作员" };
        }
        private List<UserModel> users;
        public List<UserModel> GetUsers
        {
            get
            {
                return users;
            }
            set
            {
                users = value;
            }
        }
        private UserModel _current;
        public UserModel Current
        {
            get { return _current; }
            set
            {
                _current = value;
                UserChangeEvent?.Invoke(_current);
                LoginTime = DateTime.Now;

                LogHelper.Instance.ParamChange($"【用户登录】{value.Name}");
            }
        }
        private DateTime LoginTime;
        public void Logout()
        {
            if ((DateTime.Now - LoginTime).TotalMinutes >= 5 && Current.UserType != UserType.操作工)
                Current = users.Where(p => p.Name == UserType.操作工.ToString()).FirstOrDefault();
        }
        public void Save()
        {
            SerializableHelper.Serialize(users, filePath);
        }
        public void Add(UserModel user)
        {
            if (users == null)
                users = new List<UserModel>();

            users.Add(user);
            Save();
        }
        private void Init()
        {
            users = new List<UserModel>();
            users.Add(new UserModel()
            {
                Id = 1,
                Name = UserType.操作工.ToString(),
                Pwd = "123456",
                UserType = UserType.操作工
            });
            users.Add(new UserModel()
            {
                Id = 2,
                Name = UserType.工程师.ToString(),
                Pwd = "123456",
                UserType = UserType.工程师
            });
            users.Add(new UserModel()
            {
                Id = 3,
                Name = UserType.管理员.ToString(),
                Pwd = "ld",
                UserType = UserType.管理员
            });

            Save();
        }
    }
}
